package com.raycloud.java.dada.service.impl;

import com.alibaba.fastjson.JSON;
import com.raycloud.java.dada.base.BaseService;
import com.raycloud.java.dada.base.Config;
import com.raycloud.java.dada.model.RegisterMerchant;
import com.raycloud.java.dada.model.UpdateShop;
import com.raycloud.java.dada.result.CreateShopResult;
import com.raycloud.java.dada.result.ResultModel;
import com.raycloud.java.dada.service.inter.MerchantService;
import com.raycloud.java.dada.util.BasicURL;

import java.util.List;

/**
 * Created by hechuan on 2017/6/24.
 * 商户注册和店铺接口操作实现
 */
public class MerchantServiceImpl extends BaseService implements MerchantService {

    public MerchantServiceImpl(Config config) {
        super(config);
    }

    @Override
    public ResultModel registerMerchant(RegisterMerchant registerMerchant) throws Exception {
        String result = doPost(registerMerchant, BasicURL.MERCHANT_ADD);
        ResultModel model = strToResultModel(result);

        return model;
    }

    @Override
    public CreateShopResult createShop(List<Object> createShops) throws Exception {
        String result = doPosts(createShops, BasicURL.SHOP_ADD);
        ResultModel model = strToResultModel(result);
        CreateShopResult createShopResult =null;
        if ("fail".equals(model.getStatus())) {
            //调用接口失败 返回错误信息
            throw new Exception("接口调用失败，错误代码:" + model.getCode() + "错误信息:" + model.getMsg());
        } else {
            createShopResult = JSON.parseObject(model.getResult(), CreateShopResult.class);
        }

        return createShopResult;
    }

    @Override
    public ResultModel updateShop(UpdateShop shop) throws Exception {
        String result = doPost(shop, BasicURL.SHOP_UPDATE);
        ResultModel model = strToResultModel(result);

        return model;
    }
}
